package com.health.tools;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.util.Log;

public class GetPostJTYS {
	private static final String URL = GlobalConts.getUrlPost();
//	private static final String MODIFY = "api/app/";// 修改前
	private static final String MODIFY = "api/app/jtys/";// 修改后
	// 设置密码(找回密码)
	private static final String URL_RESETER_PASSWORD = "api/app/ecg/user/resetPassword";
	// 上传文件
	public static final String URL_UPLOADFILE = "api/ecg/upload/uploadFile";
	// 升级URL
	private static final String URL_UPDATE = "versioncode.aspx";
	// 获取数据库同步
	public static final String URL_DBSYNC = "dataSync";
	// 修改密码
	private static final String URL_CHANGPWD = MODIFY +"ecg/user/modifyPassword";
	// 获取服务项目
	public static final String URL_JTYS_FWXM = MODIFY +"doctor/serve/listdoctorPad";
	// 上传签约数据接口
	public static final String URL_JTYS_SCQYSJ = MODIFY +"FamilyDoctorSigning/CustomerDoctorCont/addCustomerDoctorCont";
	// 上传服务满意度评价接口
	public static final String URL_JTYS_FWMYDPJ = MODIFY +"doctor/serve/evaluate";
	// 家庭医生创建档案接口
	public static final String URL_JTYS_CJDA = MODIFY +"doctor/customer/registerCustomerFromApp";
	// 下在签约数据接口
	public static final String URL_JTYS_XZQYSJ = MODIFY +"FamilyDoctorSigning/CustomerDoctorCont/uploadCustomerDoctorContListDataForApp";
	// 家庭医生创建档案接口(列表)
	public static final String URL_JTYS_CJDA_LIST =MODIFY + "doctor/customer/registerCustomerListFromApp";
	// 测量数据上传
	public static final String URL_JTYS_MEASURE_DATA =MODIFY + "SbclData/addSbclDataBatch";


	/** 获取输入流数据 */
	private static String readStream(InputStream inStream) throws Exception {
		StringBuffer sb = new StringBuffer();
		String readLine;
		String r = null;
		BufferedReader responseReader;
		// 处理响应流，必须与服务器响应流输出的编码一致
		responseReader = new BufferedReader(new InputStreamReader(inStream, "UTF_8"));
		while ((readLine = responseReader.readLine()) != null) {
			// sb.append(readLine).append("\n");
			sb.append(readLine);// 不需要加换行
		}
		r = sb.toString();
		responseReader.close();
		return r;
	}

	public String urlPost(String params, String surl) throws Exception {
		surl = URL + surl;
		String returnCode = null;

		params = params + "&apptype=2"; // 1 ios 2 android
		// <<<<<<< .mine
		// Log("res", "urlPost= " + surl);
		// Log.w("url-------ffffff------", surl + "?" + params);
		// =======
		// Log("res", "urlPost= " + surl);
		Log.w("url-------ffffff------", surl + "?" + params);
		// >>>>>>> .r15627
		try {
			byte[] data = params.getBytes("UTF-8");
			URL url = new URL(surl);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setConnectTimeout(30 * 1000);
			conn.setDoOutput(true);// 发送POST请求必须设置允许输出
			conn.setDoInput(true);
			conn.setUseCaches(false);// 不使用Cache
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
			conn.setRequestProperty("Charset", "UTF-8");
			conn.setRequestProperty("Content-Length", String.valueOf(data.length));
			conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
			outStream.write(data);// 以内容实体方式发送请求参数
			outStream.flush();
			outStream.close();
			// System.out.println(conn.getResponseCode()
			// + "44444444444444444444444444444444");
			// 请求成功
			if (conn.getResponseCode() == 200) {
				InputStream iStream = conn.getInputStream();
				// System.out.println("jfhjfgjf");
				returnCode = readStream(iStream);

			}

		} catch (IOException e) {

			// Log.e("message--------", e.getMessage());
			returnCode = "404";
			// returnCode = e.getMessage();
		}
		if (returnCode != null) {
			Log.w("values3333333333333333333", returnCode);
		}
		return returnCode;

	}

	public String urlUpdatePost(String params, String surl) throws Exception {
		surl = URL + surl;
		// Log("value3333333333", surl);
		String returnCode = null;
		try {
			byte[] data = params.getBytes();
			java.util.Random r = new java.util.Random();
			String par = "";
			for (int i = 0; i < 10; i++) {
				par += r.nextInt();
			}
			URL url = new URL(surl + "?r=" + par);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setConnectTimeout(30 * 1000);
			conn.setDoOutput(true);// 发送POST请求必须设置允许输出
			conn.setDoInput(true);
			conn.setUseCaches(false);// 不使用Cache
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
			conn.setRequestProperty("Charset", "UTF-8");
			conn.setRequestProperty("Content-Length", String.valueOf(data.length));
			conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
			outStream.write(data);// 以内容实体方式发送请求参数
			outStream.flush();
			outStream.close();
			// 请求成功
			if (conn.getResponseCode() == 200) {
				InputStream iStream = conn.getInputStream();
				returnCode = readStream(iStream);
			}

		} catch (IOException e) {
			returnCode = "404";
		}

		return returnCode;

	}

	// 升级
	public String Update() throws Exception {
		String params = "";
		return urlPost(params, URL_UPDATE);
	}

	// 同步数据库
	public String getDataSync(String params) throws Exception {
		return urlPost(params, URL_DBSYNC);
	}

	// 修改密码
	public String getChangePwd(String params) throws Exception {
		return urlPost(params, URL_CHANGPWD);
	}

}